<?php

namespace app\api\server;

use app\common\controller\Api;
use app\common\model\goods\Goods as GoodsGoods;
use app\common\model\UserGroup;
use think\Controller;

/**
 * 商品逻辑
 */
class Goods extends Controller
{

    /**
     * 商品列表
     *
     */
    public static function goods_list($params, $group_id)
    {
        extract($params);
        $goods = new GoodsGoods();
        if (isset($keywords) && $keywords) {
            $goods->whereLike('title', '%' . $keywords . '%');
        }
        if (isset($category_id) && $category_id) {
            $goods->where('category_id', $category_id);
        }
        if (isset($type) && $type) {
            $goods->where('type', $type);
        }
        if ($home == 1) {
            $goods->where('hot', 1);
        }
        $goods->where('status', 'normal');
        $lists = $goods->order('sales desc')->paginate();
        if ($group_id) {
            foreach ($lists as $k => $v) {
                $v->vip_price = self::vip_price($v, $group_id);
            }
        }
        return $lists;
    }

    //获取会员价格
    static  public function vip_price($goods, $group_id, $num = 1)
    {
        $group = UserGroup::get($group_id);

        if ($group->discount <= 0) {
            if ($goods->type == '1') {
                $vip_price = $goods->price;
                $discount_amount = 0;
                $performance = bcmul($goods->price * 0.58, $num, 2);
                $total_fee = bcmul($goods->price, $num, 2);
            } else {
                $total_fee = $vip_price =  bcmul($goods->min_price, $num, 2)  . '~' . bcmul($goods->max_price, $num, 2);
                $discount_amount = 0;
                $performance = 0;
            }
        } else {
            $discount = bcdiv($group->discount, 10, 2);
            if ($goods->type == '1') {
                $vip_price = bcmul($goods->price, $discount, 2);
                $total_fee = bcmul($goods->price, $num, 2);
                $performance = bcmul($vip_price, $num, 2);
                $discount_amount = bcsub($total_fee, $performance,  2);
            } else {
                $min_price = bcmul($goods->min_price, $discount, 2);
                $max_price = bcmul($goods->max_price, $discount, 2);
                $vip_price =  bcmul($min_price, $num, 2)  . '~' . bcmul($max_price, $num, 2);
                $discount_amount = bcmul(bcsub($goods->min_price, $min_price, 2), $num, 2) . '~' . bcmul(bcsub($goods->max_price, $max_price, 2), $num, 2);
                $total_fee =  bcmul($goods->min_price, $num, 2) . '~' . bcmul($goods->max_price, $num, 2);
                $performance = 0;
            }
        }

        return compact('vip_price', 'discount_amount', 'total_fee', 'performance');
    }

    /**
     * 增销减库
     * @param int $product_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function reduceInventory($goods_id,  $num)
    {

        try {
            $goods = GoodsGoods::get($goods_id);
            if ($goods->stock < $num) {
                exception('库存不足');
            }
            $goods->setInc('sales', $num);
            $goods->setDec('stock', $num);
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
    /**
     * 减销增库
     * @param int $goods_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function stockUp($goods_id, $num)
    {
        try {
            $goods = GoodsGoods::get($goods_id);

            $goods->setDec('sales', $num);
            $goods->setInc('stock', $num);
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
}
